package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Settlecategory;
import com.foreknow.entity.User;
import com.foreknow.service.SettlecategoryService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/settlecategory")
public class SettlecategoryController {
    @Autowired
    private SettlecategoryService settlecategoryService;

    @RequestMapping(value = "/findSettlecategoryByPage",method = RequestMethod.GET)
    @ResponseBody
    public String findSettleCategoryByPage(@RequestParam Map<String,Object> queryMap, @RequestParam int pageNum, @RequestParam int pageSize) throws JsonProcessingException{
        HashMap<String,Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        List<Settlecategory> settlecategoryList = settlecategoryService.getQueryAllSettlecategory();//只对第一个查询有效
        //PageInfo是PageHelper内置的分页信息类
        PageInfo<User> info = new PageInfo(settlecategoryList);
        hs.put("total",info.getTotal());
        hs.put("data",info.getList());
        if (settlecategoryList.size() > 0){
            hs.put("meta",new Constance("成功",200));
        }else{
            hs.put("meta",new Constance("失败",201));
        }
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteSettlecategoryById",method = RequestMethod.GET)
    @ResponseBody
    private String deleteSettleCategoryById(@RequestParam int userId)throws JsonProcessingException{
        int i = settlecategoryService.deleteSettlecategory(userId);
        HashMap<String,Object> hs = new HashMap<>();
        if(i!=1) {
            hs.put("meta", new Constance("msg", 201));
        }else{
            hs.put("meta", new Constance("msg", 200));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    private String addSettleCategory(@RequestBody Settlecategory settle)throws JsonProcessingException{
        int i=settlecategoryService.addSettlecategory(settle);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",settle);
        if(i!=1) {
            hs.put("meta", new Constance("添加失败", 201));
        }else{
            hs.put("meta", new Constance("添加成功", 200));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ResponseBody
    public String selectSettleCategoryById(@RequestParam Integer userId) throws JsonProcessingException {
        Settlecategory settlecategory = settlecategoryService.selectSettlecategory(userId);
        HashMap<String, Object> hs = new HashMap<>();
        if (settlecategory != null) {
            hs.put("data", settlecategory);
            hs.put("meta", new Constance("查询结算登记成功", 200));
        } else {
            hs.put("meta", new Constance("查询结算等级失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    private String updateSettleCategory(@RequestBody Settlecategory settlecategory)throws JsonProcessingException{
        int i=settlecategoryService.updateSettlecategory(settlecategory);
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data",settlecategory);
        if(i!=1) {
            hs.put("meta", new Constance("修改结算等级失败", 201));
        }else{
            hs.put("meta", new Constance("修改结算等级成功", 200));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteList", method = RequestMethod.POST)
    @ResponseBody
    public String deleteList(@RequestBody() Object[] param) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if (param != null) {
            for (int i = 0; i < param.length; i++) {
                settlecategoryService.deleteSettlecategory((Integer) param[i]);
            }
            hs.put("meta", new Constance("批量删除成功", 200));
        } else {
            hs.put("meta", new Constance("批量删除失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}